<template>
  <div class="personal-info">
    <h2>个人信息</h2>
    <el-form :model="form" label-width="120px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="会员等级">
        <el-input v-model="form.memberName" disabled></el-input>
      </el-form-item>
      <el-form-item label="会员积分">
        <el-input v-model="form.vipPoint" disabled></el-input>
      </el-form-item>
      <el-form-item label="余额">
        <el-input v-model="form.balance" disabled></el-input>
      </el-form-item>
      <!-- <el-form-item label="会员积分">
        <el-input v-model="form.rcPoint" disabled></el-input>
      </el-form-item>           -->
      <el-form-item label="软件到期时间">
        <el-input v-model="form.memberExpiredate" disabled></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateInfo">更新信息</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';
import { IHuoQuJiFenModel, IHuoQuJiFenData, ICheckUserNameResult, GetBaseURL } from '../../composables/types';
import { setCookie, getCookie } from '../../composables/utils';

// 接口地址
const baseUrl: string = GetBaseURL();
        // const baseUrl: string = "https://api.bishi.turuio.net"; //测试

const form = reactive({
  username: '用户名',
  email: 'example@email.com',
  phone: '13800138000',
  memberName: '',
  vipPoint: 0,
  rcPoint: 0,
  memberExpiredate: '',
  balance: 1
});

const updateInfo = () => {
  // 这里添加更新个人信息的逻辑
  form.username = getCookie('TR_username') || '';
  form.email = getCookie('TR_email') || '';
  form.phone = getCookie('TR_mobile') || '';

  huoQuJiFen().then((result) => {
    let data: IHuoQuJiFenModel = result;
    if (!data) {
      console.error('数据为空');
    } else {
      form.memberName = data.data.memberName;
      form.vipPoint = data.data.vip_point;
      form.rcPoint = data.data.rc_point;
      form.memberExpiredate = data.data.memberExpiredate;
      form.balance = data.data.rc_balance;
    }
  }).catch((error) => {
    console.error('获取积分失败:', error);
  });
  ElMessage.success('个人信息更新成功');
};

/**
 * 检查用户令牌是否有效
 *
 * 此函数通过发送GET请求到后端服务器来验证用户的令牌是否有效。
 * 如果令牌有效，则返回true；否则返回false。
 *
 * @returns {Promise<boolean>} 返回一个Promise对象，该对象解析为布尔值表示令牌是否有效
 */
async function tokenCheck(): Promise<boolean> {
  return axios.get(`${baseUrl}/api/token/check`, {
    headers: {
      'Content-Type': 'application/json',
      'token': getCookie('TR_token'),
    },
  })
    .then((response) => {
      // 处理响应数据
      const data = response.data as ICheckUserNameResult;
      if (data.code === 1) {
        return true;
      } else {
        return false;
      }
    })
    .catch((error) => {
      // 处理错误
      console.error('检查令牌失败:', error);
      return false;
    });
}

/**
 * 获取用户积分
 * @returns {Promise<IHuoQuJiFenModel>} 返回一个包含用户积分信息的 Promise 对象
 */
async function huoQuJiFen(): Promise<IHuoQuJiFenModel> {
  let result: IHuoQuJiFenModel;

  try {
    // 向后端发送获取用户信息的请求
    const response = await axios.post(`${baseUrl}/api/user/getUserInfo`, {}, {
      headers: {
        'Content-Type': 'application/json',
        'token': getCookie('TR_token'),
      },
    });
    // 解析并返回用户积分信息
    result = response.data;
    return result;
  } catch (error) {
    // 异常情况下构造错误的用户积分信息对象
    throw error; // 或者根据业务逻辑处理错误
  }
};

onMounted(() => {
  form.username = getCookie('TR_username') ?? '';
  form.email = getCookie('TR_email') ?? '';
  form.phone = getCookie('TR_mobile') ?? '';

  huoQuJiFen().then((result) => {
    let data: IHuoQuJiFenModel = result;

    if (!data) {
      console.error('数据为空');
    } else {
      form.memberName = data.data.memberName;
      form.vipPoint = data.data.vip_point;
      form.rcPoint = data.data.rc_point;
      form.memberExpiredate = data.data.memberExpiredate;
      form.balance = data.data.rc_balance;
    }
  }).catch((error) => {
    console.error('获取积分失败:', error);
  });
});

</script>
